package jp.tmhouse.macdorder;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.hardware.SensorManager;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MacdOrderActivity extends Activity {
	private LayoutInflater 		m_inflater;
	private ViewGroup 			m_itemsView;
	private Button 				m_calcBtn;;
	private ArrayList<MacdItemView>	 m_items;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_macd_order);
        
        m_inflater = (LayoutInflater)getSystemService(
        		Context.LAYOUT_INFLATER_SERVICE);
        
        m_calcBtn = (Button)findViewById(R.id.calcBtn);
        m_calcBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showResult();
			}
        });
        
        m_itemsView = (ViewGroup)findViewById(R.id.itemsView);
        m_items = new ArrayList<MacdItemView>(50);
        
        addItem(m_itemsView, "ベーコンレタス\nバーガー", R.drawable.image_baconlettuceburger, "100円");
        addItem(m_itemsView, "ビックチキン", R.drawable.image_bigchicken, "101円");
        addItem(m_itemsView, "ビックマック", R.drawable.image_bigmac, "102円");
        addItem(m_itemsView, "チーズバーガー", R.drawable.image_cheeseburger, "103円");
        addItem(m_itemsView, "チーズ月見", R.drawable.image_cheesetsukimi, "103円");
        addItem(m_itemsView, "チキンクリスプ", R.drawable.image_chickencrisp, "103円");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_macd_order, menu);
        return true;
    }
    
    
    private void showResult() {
    	StringBuilder sb = new StringBuilder();
    	
		sb.append("注文お願いします。\n\n");
    	int cnt = 0;
    	for( MacdItemView item : m_items ) {
    		if( item.isBuy() ) {
        		if( cnt != 0 ) {
        			sb.append("、\n");
        		}
    			sb.append("  " + item.getName());
    			cnt++;
    		}
    	}
   		if( cnt != 0 ) {
  			sb.append("、\n");
   		}
		sb.append("スマイル");
		sb.append("\n\nをください。");
		
		showDialog(sb.toString());
    }
    
    private void showDialog(String msg) {
    	SensorManager smgr = (SensorManager)getSystemService(
    			Activity.SENSOR_SERVICE);

    	WindowManager wm = (WindowManager)getSystemService(
    			Activity.WINDOW_SERVICE);
    	Display disp = wm.getDefaultDisplay();
    	
    	TmBalloon tb = new TmBalloon(this, 30, disp.getWidth() - 5, smgr);
    	tb.cancelAutoDismiss();
    	tb.setText(msg);
    	tb.show();
    }
    
    /**
     * アイテム追加.
     * @param parent
     */
    private void addItem(ViewGroup parent, String name, int imgResId, String price) {
    	MacdItemView v = new MacdItemView(this, name, imgResId, price);
    	m_items.add(v);
    	parent.addView(v);
    }
    
    /**
     * 
     * @author mutoh
     *
     */
    private class MacdItemView extends LinearLayout {
    	private String m_price;
    	private String m_name;
    	private CheckBox	m_buyCbx;

		public MacdItemView(Context context, String name, int imgResId, String price) {
			super(context);
			this.setOrientation(LinearLayout.HORIZONTAL);
			m_price = price;
			m_name = name;
			
			View v = (View)m_inflater.inflate(R.layout.item, null);
			
			ImageView imgView = (ImageView)v.findViewById(R.id.itemImg);
			imgView.setImageResource(imgResId);
			imgView.setContentDescription(name);
			
			TextView nameView = (TextView)v.findViewById(R.id.itemName);
			nameView.setText(name);
			
			TextView priceView = (TextView)v.findViewById(R.id.itemPrice);
			priceView.setText(price);
			
			m_buyCbx = (CheckBox)v.findViewById(R.id.buyCbx);
			this.addView(v);
		}
    	
		public String getPrice() {
			return(m_price);
		}
		public String getName() {
			return(m_name);
		}
		public boolean isBuy() {
			return(m_buyCbx.isChecked());
		}
    }
}
